package com.by.toolkitspring.dubbo.qosCommand;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.deploy.ApplicationDeployer;
import org.apache.dubbo.qos.api.Cmd;
import org.apache.dubbo.qos.api.PermissionLevel;
import org.apache.dubbo.qos.command.impl.BaseOffline;
import org.apache.dubbo.rpc.model.FrameworkModel;

/**
 * 自定义qos命令
 *
 * @author Tian Wei
 * @version 1.0
 * @date 2024/2/6 2:04 PM
 */
@Slf4j
@Cmd(
        name = "byOffline",
        summary = "byOffline dubbo",
        example = {"byOffline dubbo", "byOffline xx.xx.xxx.service"},
        requiredPermissionLevel = PermissionLevel.PUBLIC)
public class ByOffline extends BaseOffline {
    private ApplicationDeployer applicationDeployer;

    public ByOffline(FrameworkModel frameworkModel) {
        super(frameworkModel);
        applicationDeployer = frameworkModel.getInternalApplicationModel().getDeployer();

    }

    protected boolean doExecute(String servicePattern) {
        //强制移除dubbo的注销钩子
//        DubboShutdownHook dubboShutdownHook = (DubboShutdownHook) ReflectUtil.getFieldValue(applicationDeployer, "dubboShutdownHook");
//        dubboShutdownHook.unregister();
//        log.info("[ByOffline][doUnexport] 强制结束钩子");
        log.info("[ByOffline][doExecute] 服务通知下线:{}", applicationDeployer.getApplicationModel().getApplicationName());
        return super.offline(servicePattern);
    }

}
